package com.jjx.trainingapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjx.trainingapi.common.BaseColumns;
import com.jjx.trainingapi.entity.param.DcUserFriendsParam;
import com.jjx.trainingapi.entity.po.DcUserFriends;
import com.jjx.trainingapi.entity.po.DcUserFriendsImgList;
import com.jjx.trainingapi.entity.po.DcUserFriendsLike;
import com.jjx.trainingapi.entity.vo.CommentListVo;
import com.jjx.trainingapi.entity.vo.FriendsListVo;
import com.jjx.trainingapi.mapper.DcUserFriendsMapper;
import com.jjx.trainingapi.service.DcUserFriendsImgListService;
import com.jjx.trainingapi.service.DcUserFriendsLikeService;
import com.jjx.trainingapi.service.DcUserFriendsService;
import com.jjx.trainingapi.util.LoginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LoveHuahua
 * @date 2022年06月03日 23:27
 * @description believe in yourself
 */
@Service
public class DcUserFriendsServiceImpl extends ServiceImpl<DcUserFriendsMapper, DcUserFriends> implements DcUserFriendsService {

    @Autowired
    DcUserFriendsImgListService friendsImgListService;
    @Autowired
    DcUserFriendsLikeService dcUserFriendsLikeService;

    /**
     * 获取列表
     *
     * @return
     */
    @Override
    public List<FriendsListVo> getList() {
        return baseMapper.getList(LoginManager.getUserId());
    }

    /**
     * 发布一条朋友圈
     *
     * @param friends
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publishFriends(DcUserFriendsParam friends) {
        friends.setUserId(LoginManager.getUserId());
        //保存文字
        save(friends);
        //保存图片
        for (String s : friends.getImgList()) {
            DcUserFriendsImgList entity = new DcUserFriendsImgList();
            entity.setFriendsId(friends.getId());
            entity.setUrl(s);
            friendsImgListService.save(entity);
        }
    }

    /**
     * 获取评论列表
     *
     * @param friends
     * @return
     */
    @Override
    public List<CommentListVo> getCommentList(DcUserFriendsParam friends) {
        return baseMapper.getCommentList(friends);
    }

    /**
     * 添加收藏
     *
     * @param friends
     */
    @Override
    public void addFavorite(DcUserFriendsParam friends) {
        DcUserFriendsLike one = dcUserFriendsLikeService.lambdaQuery()
                .eq(DcUserFriendsLike::getUserId, LoginManager.getUserId())
                .eq(DcUserFriendsLike::getFriendsId, friends.getId())
                .one();
        if (one == null){
            DcUserFriendsLike entity = new DcUserFriendsLike();
            entity.setFriendsId(friends.getId());
            entity.setUserId(LoginManager.getUserId());
            dcUserFriendsLikeService.save(entity);
        }

    }

    /**
     * 移除收藏
     *
     * @param friends
     */
    @Override
    public void removeFavorite(DcUserFriendsParam friends) {
        dcUserFriendsLikeService.lambdaUpdate().eq(DcUserFriendsLike::getUserId, LoginManager.getUserId())
                .eq(DcUserFriendsLike::getFriendsId, friends.getId()).remove();
    }

    /**
     * 添加分享数
     *
     * @param friends
     */
    @Override
    public void addShard(DcUserFriendsParam friends) {
        //朋友圈
        DcUserFriends one = lambdaQuery().eq(BaseColumns::getId, friends.getId()).one();
        lambdaUpdate().eq(DcUserFriends::getId, one.getId())
                .set(DcUserFriends::getShardCount, one.getShardCount() + 1)
                .update();
    }
}
